package com.yonyou.pmclouds.safetypatrol.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.SqlUtil;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.entity.StandardType;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolDetailMapper;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolMapper;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import org.apache.commons.collections.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = SafetypatrolQuery.class)
public class SafetypatrolQueryImpl implements SafetypatrolQuery {


    @Resource
    private SafetypatrolMapper safetypatrolMapper;
    @Resource
    private SafetypatrolDetailMapper safetypatrolDetailMapper;
    @Resource
    private PictureRemoteService pictureRemoteService;
//    @Resource
//    private SafetypatrolDetailQuery detailQuery;



    //分页查询
    @Override
    public PageObject<SafetypatrolVO> queryByCondition(String keyword, Integer pageIndex, Integer pageSize, byte stateCode) throws BusinessException {



        List<SafetypatrolVO> safetypatrolList = new ArrayList<>();
        List<String> needPKs =new ArrayList<>();


        if (stateCode==0){
            throw new BusinessException("停用状态的检查标准无法查询");
        }


        int total = safetypatrolMapper.countPage(keyword, RuntimeEnvironment.getTenantId(), stateCode);
        if (total > 0) {
            safetypatrolList = safetypatrolMapper.queryByPage(keyword, RuntimeEnvironment.getTenantId(), pageIndex, pageSize, stateCode);
        }
            //为所属分类赋值
            if (CollectionUtils.isNotEmpty(safetypatrolList)){
                for (SafetypatrolVO safetypatrolVO : safetypatrolList) {
                    checkSafetypatrolVOType(safetypatrolVO);

                }

                for (SafetypatrolVO safetypatrolVO : safetypatrolList) {
                    needPKs.add(safetypatrolVO.getPkSafetypatrol());
                }

                String[] stringPKs = needPKs.toArray(new String[0]);

                List<SafetypatrolDetailVO> safetypatrolDetailVOList = safetypatrolDetailMapper.selectListSafeVO(SqlUtil.getSqlIn(stringPKs));

                for (SafetypatrolVO safetypatrolVO : safetypatrolList) {
                    long count = safetypatrolDetailVOList.stream().
                            filter(s -> s.getPkSafetypatrol().equals(safetypatrolVO.getPkSafetypatrol())).
                            count();
                    safetypatrolVO.setDetailcount(new Long(count).intValue());

                }

            }

            //集合转数组
        SafetypatrolVO[] safetypatrolVOS = safetypatrolList.toArray(new SafetypatrolVO[0]);
        PageObject<SafetypatrolVO> pageObject = new PageObject<>(safetypatrolVOS, total, 0);

        return pageObject;

    }



    @Override
    public SafetypatrolVO queryById(String pk_safetypatrol)throws BusinessException {
        Map map =new HashMap();
        map.put("pk_safetypatrol", pk_safetypatrol);
        SafetypatrolVO safetypatrolVO= safetypatrolMapper.findById(map);

        return safetypatrolVO;
    }

    @Override
    public SafetypatrolVO findDetail(String pk_safetypatrol) throws BusinessException {

        SafetypatrolVO safetypatrolVO =safetypatrolMapper.getById(pk_safetypatrol);

        List<SafetypatrolDetailVO> safetypatrolDetailVOList =safetypatrolDetailMapper.findById(pk_safetypatrol);

        if (CollectionUtils.isEmpty(safetypatrolDetailVOList)){
            return null;
        }

        for (SafetypatrolDetailVO detailVO : safetypatrolDetailVOList) {
            PictureVO[] pictureVOS = pictureRemoteService.queryExamplePicsByEntity(detailVO.getPkSafetypatroldetail());
            detailVO.setExamplePictures(pictureVOS);
        }



        safetypatrolVO.setSafeDetailVOS(safetypatrolDetailVOList.toArray(new SafetypatrolDetailVO[0]));
        return safetypatrolVO;

    }

    @Override
    public List<SafetypatrolVO> queryByTenant(String pkTenant) {

        return safetypatrolMapper.queryByTenant(pkTenant);
    }


    public void checkSafetypatrolVOType(SafetypatrolVO safetypatrolVO)throws BusinessException {
        //换为常量
        if (safetypatrolVO.getStandardType()==1){
            safetypatrolVO.setStandardtypename(StandardType.COMMONCHECK);
        }else if(safetypatrolVO.getStandardType()==2){
            safetypatrolVO.setStandardtypename(StandardType.WEEKCHECK);
        }else if(safetypatrolVO.getStandardType()==3){
            safetypatrolVO.setStandardtypename(StandardType.MONTHCHECK);
        }else {
            safetypatrolVO.setStandardtypename(StandardType.QUARTERCHECK);
        }
    }
}


